import os
from collections.abc import Generator

import pytest

from core.model_runtime.entities.llm_entities import LLMResult, LLMResultChunk, LLMResultChunkDelta
from core.model_runtime.entities.message_entities import (
    AssistantPromptMessage,
    ImagePromptMessageContent,
    SystemPromptMessage,
    TextPromptMessageContent,
    UserPromptMessage,
)
from core.model_runtime.errors.validate import CredentialsValidateFailedError
from core.model_runtime.model_providers.ollama.llm.llm import OllamaLargeLanguageModel


def test_validate_credentials():
    model = OllamaLargeLanguageModel()

    with pytest.raises(CredentialsValidateFailedError):
        model.validate_credentials(
            model='mistral:text',
            credentials={
                'base_url': 'http://localhost:21434',
                'mode': 'chat',
                'context_size': 2048,
                'max_tokens': 2048,
            }
        )

    model.validate_credentials(
        model='mistral:text',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'chat',
            'context_size': 2048,
            'max_tokens': 2048,
        }
    )


def test_invoke_model():
    model = OllamaLargeLanguageModel()

    response = model.invoke(
        model='mistral:text',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'chat',
            'context_size': 2048,
            'max_tokens': 2048,
        },
        prompt_messages=[
            UserPromptMessage(
                content='Who are you?'
            )
        ],
        model_parameters={
            'temperature': 1.0,
            'top_k': 2,
            'top_p': 0.5,
            'num_predict': 10
        },
        stop=['How'],
        stream=False
    )

    assert isinstance(response, LLMResult)
    assert len(response.message.content) > 0


def test_invoke_stream_model():
    model = OllamaLargeLanguageModel()

    response = model.invoke(
        model='mistral:text',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'chat',
            'context_size': 2048,
            'max_tokens': 2048,
        },
        prompt_messages=[
            SystemPromptMessage(
                content='You are a helpful AI assistant.',
            ),
            UserPromptMessage(
                content='Who are you?'
            )
        ],
        model_parameters={
            'temperature': 1.0,
            'top_k': 2,
            'top_p': 0.5,
            'num_predict': 10
        },
        stop=['How'],
        stream=True
    )

    assert isinstance(response, Generator)

    for chunk in response:
        assert isinstance(chunk, LLMResultChunk)
        assert isinstance(chunk.delta, LLMResultChunkDelta)
        assert isinstance(chunk.delta.message, AssistantPromptMessage)


def test_invoke_completion_model():
    model = OllamaLargeLanguageModel()

    response = model.invoke(
        model='mistral:text',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'completion',
            'context_size': 2048,
            'max_tokens': 2048,
        },
        prompt_messages=[
            UserPromptMessage(
                content='Who are you?'
            )
        ],
        model_parameters={
            'temperature': 1.0,
            'top_k': 2,
            'top_p': 0.5,
            'num_predict': 10
        },
        stop=['How'],
        stream=False
    )

    assert isinstance(response, LLMResult)
    assert len(response.message.content) > 0


def test_invoke_stream_completion_model():
    model = OllamaLargeLanguageModel()

    response = model.invoke(
        model='mistral:text',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'completion',
            'context_size': 2048,
            'max_tokens': 2048,
        },
        prompt_messages=[
            SystemPromptMessage(
                content='You are a helpful AI assistant.',
            ),
            UserPromptMessage(
                content='Who are you?'
            )
        ],
        model_parameters={
            'temperature': 1.0,
            'top_k': 2,
            'top_p': 0.5,
            'num_predict': 10
        },
        stop=['How'],
        stream=True
    )

    assert isinstance(response, Generator)

    for chunk in response:
        assert isinstance(chunk, LLMResultChunk)
        assert isinstance(chunk.delta, LLMResultChunkDelta)
        assert isinstance(chunk.delta.message, AssistantPromptMessage)


def test_invoke_completion_model_with_vision():
    model = OllamaLargeLanguageModel()

    result = model.invoke(
        model='llava',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'completion',
            'context_size': 2048,
            'max_tokens': 2048,
        },
        prompt_messages=[
            UserPromptMessage(
                content=[
                    TextPromptMessageContent(
                        data='What is this in this picture?',
                    ),
                    ImagePromptMessageContent(
                        data=''
                    )
                ]
            )
        ],
        model_parameters={
            'temperature': 0.1,
            'num_predict': 100
        },
        stream=False,
    )

    assert isinstance(result, LLMResult)
    assert len(result.message.content) > 0


def test_invoke_chat_model_with_vision():
    model = OllamaLargeLanguageModel()

    result = model.invoke(
        model='llava',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'chat',
            'context_size': 2048,
            'max_tokens': 2048,
        },
        prompt_messages=[
            UserPromptMessage(
                content=[
                    TextPromptMessageContent(
                        data='What is this in this picture?',
                    ),
                    ImagePromptMessageContent(
                        data=''
                    )
                ]
            )
        ],
        model_parameters={
            'temperature': 0.1,
            'num_predict': 100
        },
        stream=False,
    )

    assert isinstance(result, LLMResult)
    assert len(result.message.content) > 0


def test_get_num_tokens():
    model = OllamaLargeLanguageModel()

    num_tokens = model.get_num_tokens(
        model='mistral:text',
        credentials={
            'base_url': os.environ.get('OLLAMA_BASE_URL'),
            'mode': 'chat',
            'context_size': 2048,
            'max_tokens': 2048,
        },
        prompt_messages=[
            UserPromptMessage(
                content='Hello World!'
            )
        ]
    )

    assert isinstance(num_tokens, int)
    assert num_tokens == 6
